package com.pivotal.cloud.security.jwt;

import com.pivotal.cloud.crypto.codec.Codecs;
import com.pivotal.cloud.crypto.sign.Signer;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @className: JwtHeaderHelper
 * @projectName: 项目
 * @module: 项目-JwtHeaderHelper类，主要位于模块
 * @content:
 * @author: Powered by Marklin
 * @datetime: 2025-06-21 20:40
 * @version:
 * @copyright: Copyright © 2018-2025 Systems Incorporated. All rights reserved.
 */
public class JwtHeaderHelper {

	JwtHeaderHelper() {
	}

	static JwtHeader create(String header) {
		byte[] bytes = Codecs.b64UrlDecode(header);
		return new JwtHeader(bytes, parseParams(bytes));
	}

	static JwtHeader create(Signer signer, Map<String, String> params) {
		Map<String, String> map = new LinkedHashMap(params);
		map.put("alg", JwtAlgorithms.sigAlg(signer.algorithm()));
		HeaderParameters p = new HeaderParameters(map);
		return new JwtHeader(serializeParams(p), p);
	}

	static HeaderParameters parseParams(byte[] header) {
		Map<String, String> map = parseMap(Codecs.utf8Decode(header));
		return new HeaderParameters(map);
	}

	private static Map<String, String> parseMap(String json) {
		if (json != null) {
			json = json.trim();
			if (json.startsWith("{")) {
				return parseMapInternal(json);
			}

			if (json.equals("")) {
				return new LinkedHashMap();
			}
		}

		throw new IllegalArgumentException("Invalid JSON (null)");
	}

	private static Map<String, String> parseMapInternal(String json) {
		Map<String, String> map = new LinkedHashMap();
		json = trimLeadingCharacter(trimTrailingCharacter(json, '}'), '{');
		String[] var2 = json.split(",");
		int var3 = var2.length;

		for (int var4 = 0; var4 < var3; ++var4) {
			String pair = var2[var4];
			String[] values = pair.split(":");
			String key = strip(values[0], '"');
			String value = null;
			if (values.length > 0) {
				value = strip(values[1], '"');
			}

			if (map.containsKey(key)) {
				throw new IllegalArgumentException("Duplicate '" + key + "' field");
			}

			map.put(key, value);
		}

		return map;
	}

	private static String strip(String string, char c) {
		return trimLeadingCharacter(trimTrailingCharacter(string.trim(), c), c);
	}

	private static String trimTrailingCharacter(String string, char c) {
		return string.length() >= 0 && string.charAt(string.length() - 1) == c
				? string.substring(0, string.length() - 1) : string;
	}

	private static String trimLeadingCharacter(String string, char c) {
		return string.length() >= 0 && string.charAt(0) == c ? string.substring(1) : string;
	}

	private static byte[] serializeParams(HeaderParameters params) {
		StringBuilder builder = new StringBuilder("{");
		appendField(builder, "alg", params.alg);
		params.getClass();
		if ("JWT" != null) {
			params.getClass();
			appendField(builder, "typ", "JWT");
		}

		Iterator var2 = params.map.entrySet().iterator();

		while (var2.hasNext()) {
			Map.Entry<String, String> entry = (Map.Entry) var2.next();
			appendField(builder, (String) entry.getKey(), (String) entry.getValue());
		}

		builder.append("}");
		return Codecs.utf8Encode(builder.toString());
	}

	private static void appendField(StringBuilder builder, String name, String value) {
		if (builder.length() > 1) {
			builder.append(",");
		}

		builder.append("\"").append(name).append("\":\"").append(value).append("\"");
	}

}
